#include "py32f0xx_hal.h"
#include "key.h"

UserKey_t UserKey;
/********************************************************************************
Description:
Input:
Output:
Return:
*********************************************************************************/
void KeyInit(void)
{
	GPIO_InitTypeDef GPIO_InitStruct={0};

	/* Enable the BUTTON Clock */
	KEY_CLK_ENABLE();

	GPIO_InitStruct.Pin = KEY_PIN;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;

	/* Configure Button pin as input */
	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

	HAL_GPIO_Init(KEY_PORT, &GPIO_InitStruct);

// 	LL_EXTI_SetEXTISource(LL_EXTI_CONFIG_PORTF,LL_EXTI_CONFIG_LINE4);

//	LL_EXTI_InitTypeDef EXTI_InitStruct;
//	EXTI_InitStruct.Line = KEY_EXTI_LINE;
//	EXTI_InitStruct.LineCommand = ENABLE;
//	EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
//	EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;							//下降沿
//	LL_EXTI_Init(&EXTI_InitStruct);

//	NVIC_SetPriority(KEY_IRQ, 2);
//	//NVIC_EnableIRQ(KEY_IRQ);
//	NVIC_DisableIRQ(KEY_IRQ);													//先禁止中断，休眠前再打开
}
/********************************************************************************
Description:    按键任务 40ms调用周期
Input:
Output:
Return:
*********************************************************************************/
void KeyTask(void)
{
	static uint16_t KeyPressFilterTime = 0;
	static uint16_t KeyShortPressFlag = 0,KeyLongPressFlag = 0;
	//1 ms读取
	if (HAL_GPIO_ReadPin(KEY_PORT,KEY_PIN) == KEY_PRESS_FLAG)
	{
		KeyPressFilterTime++;
		if (KeyPressFilterTime > KEY_LONG_PRESS_TIME) 
		{
			//长按不需要释放
			UserKey.KeyCmd = KEY_EVEN_LONG_PRESS;
			KeyShortPressFlag = 0;
			KeyLongPressFlag = 1;

		}
		else
		if (KeyPressFilterTime > KEY_SHORT_PRESS_TIME) 						
		{
			KeyShortPressFlag = 1;
			KeyLongPressFlag = 0;
		}
		else
		{
			KeyShortPressFlag = 0;
			KeyLongPressFlag = 0;
		}

	}
	else
	{
		KeyPressFilterTime = 0;
		//短按都要释放
		if(KeyShortPressFlag == 1) 
		{
			UserKey.KeyCmd = KEY_EVEN_PRESS;
			KeyShortPressFlag = 0;
		}
	}

}

/********************************************************************************
Description:
Input:
Output:
Return:
*********************************************************************************/
